package org.manager.authorize;

import static org.apache.commons.lang.StringUtils.isNotBlank;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.manager.generic.web.JsonViewUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/authorize")
public class AuthorizeController {
	
	@Resource(name="authorizeRepository")
	private AuthorizeRepository authorizeRep;
	
	private static final String roleAllField[] = new String[]{"id","name","status","remark"};
	private static final String userAllField[] = new String[]{"id","loginName","loginPasswd","userName","mobilePhone","phone","email","fax","status","manager","groupId","groupCode"};

	@RequestMapping(params="method=findUserByUnit")
	public ModelAndView findUserByUnit(String unitId){
		return JsonViewUtil.getSuccessViewWithIncludProperty(authorizeRep.findUserByUnit(unitId), userAllField);
	}
	
	@RequestMapping(params="method=addUser")
	public ModelAndView addUser(UserAccount user){
		if(user != null && isNotBlank(user.getGroupId()) && isNotBlank(user.getGroupCode())){
			authorizeRep.addUserAccount(user);
			return JsonViewUtil.getSuccessView();
		}else{
			return JsonViewUtil.getFailureView();
		}
	}
	
	@RequestMapping(params="method=loadUser")
	public ModelAndView loadUser(String id){
		if(isNotBlank(id)){
			return JsonViewUtil.getSuccessViewWithIncludProperty(authorizeRep.loadUser(id), userAllField);
		}else{
			return JsonViewUtil.getFailureView();
		}
	}
	
	@RequestMapping(params="method=modifyUser")
	public ModelAndView modifyUser(UserAccount user){
		if(user != null && isNotBlank(user.getId())){
			authorizeRep.modifyUser(user);
			return JsonViewUtil.getSuccessView();
		}else{
			return JsonViewUtil.getFailureView();
		}
	}
	
	@RequestMapping(params="method=deleteUser")
	public ModelAndView deleteUser(String ids[]){
		Map<String,Object> map = new HashMap<String,Object>();
		int result = authorizeRep.batchDeleteUserAccount(ids);
		if(ids.length == result){
			map.put("success", "true");
		}else if(result < ids.length && result > 0){
			map.put("success", "true");
			map.put("notall", "true");
		}else{
			map.put("failure", "true");
		}
		return JsonViewUtil.getSuccessViewWithIncludProperty(map, new String[0]);
	}
	
	//======== Role ==============
	
	@RequestMapping(params="method=getAllRole")
	public ModelAndView getAllRole(){
		return JsonViewUtil.getSuccessViewWithIncludProperty(authorizeRep.getAllRole(), roleAllField);
	}
	
	@RequestMapping(params="method=addRole")
	public ModelAndView addRole(SystemRole role){
		String id = (String)authorizeRep.addRole(role);
		if(isNotBlank(id)){
			return JsonViewUtil.getSuccessView();
		}else{
			return JsonViewUtil.getFailureView();
		}
	}
	
	@RequestMapping(params="method=loadRole")
	public ModelAndView loadRole(String roleId){
		if(isNotBlank(roleId)){
			return JsonViewUtil.getSuccessViewWithIncludProperty(authorizeRep.loadRoleById(roleId), roleAllField);
		}else{
			return JsonViewUtil.getFailureView();
		}
	}
	
	@RequestMapping(params="method=deleteRole")
	public ModelAndView deleteRole(String[] roleIds){
		authorizeRep.batchDeleteRoleById(roleIds);
		return JsonViewUtil.getSuccessView();
	}
	
	@RequestMapping(params="method=modifyRole")
	public ModelAndView modifyRole(SystemRole role){
		authorizeRep.modifyRole(role);
		return JsonViewUtil.getSuccessView();
	}
	
	@RequestMapping(params="method=authorizeAccount")
	public ModelAndView authorizeAccount(String accountIds, String roleIds){
		//将所选用户添加所选角色.
		authorizeRep.authorizeRole(accountIds.split(","), roleIds.split(","));
		return JsonViewUtil.getSuccessView();
	}
	
	@RequestMapping(params="method=getAccountRole")
	public ModelAndView getAccountRole(String accountId){
		List<SystemRole> roleL;
		if(isNotBlank(accountId)){
			roleL = authorizeRep.getAccountsRoles(accountId);
			return JsonViewUtil.getSuccessViewWithIncludProperty(roleL, roleAllField);
		}else{
			roleL = authorizeRep.getAllRole();
			return JsonViewUtil.getSuccessViewWithIncludProperty(roleL, roleAllField);
		}
	}
	
	@RequestMapping(params="method=authorizeRole")
	public ModelAndView authorizeRole(String funData, String roleData){
		System.out.println("funData:"+funData);
		System.out.println("roleData:"+roleData);
		
		List<Function> funList = new ArrayList<Function>();
		String menuData[] = funData.split(":");
		String roleArray[] = roleData.split(",");
		
		for(String smenu : menuData){
			String menu[] =smenu.split(",");
			if(menu.length == 6){
				Function fun = new Function();
				fun.setMenuId(menu[0]);
				fun.setAdd(menu[1]);
				fun.setDel(menu[2]);
				fun.setModify(menu[3]);
				fun.setImp(menu[4]);
				fun.setExp(menu[5]);
				funList.add(fun);
			}
		}
		
		if(funList.size() > 0 && roleArray.length > 0){
			authorizeRep.roleAuthority(funList, roleArray);
		}
		return JsonViewUtil.getSuccessView();
	}
}
